//
//  RelatedView.m
//  NewMyMusicAPP
//
//  Created by dlios on 15/6/19.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RelatedView.h"

@implementation RelatedView




- (void)dealloc
{
    [_array release];
    [_myTableView release];
    [_type release];
    [_Id release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame Id:(NSString *)Id type:(NSString *)type
{
    self = [super initWithFrame:frame];
    if (self) {
        self.Id = Id;
        self.type = type;
        [self createView];
    }
    
    return self;
}


-(void)createView
{
    self.line = [[[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT * 55, MYWIDTH, 1)]autorelease];
    self.line.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.3];
    
    
    
    self.myTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT * 56, MYWIDTH, HEIGHT * 300) style:UITableViewStylePlain]autorelease];
    self.myTableView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.myTableView];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    self.myTableView.showsVerticalScrollIndicator = NO;//取消纵向滑条
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消cell之间的线
    
    [self asker];
    
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    //数组保护
    if (self.array.count != 0) {
        //点击相应视频.把id用协议传回视频播放
        VideoPlayerModel *videoPlayer = [[[VideoPlayerModel alloc] init]autorelease];
        videoPlayer = [self.array objectAtIndex:indexPath.row];
        [self.myDelegate refresh:videoPlayer.modelId];
        
        //点击相应视频.id更改 重新请求数据
        
        self.Id = videoPlayer.modelId;
        self.type = @"VIDEO";
        [self asker];
        

    }
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (HEIGHT * 110);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.array.count != 0) {
        return self.array.count;
    }else
    {
        return 0;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string = @"cell";
    RelatedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        cell = [[RelatedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消点击效果
    
    //渐变效果
    
    cell.layer.shadowColor = [[UIColor clearColor]CGColor];
    cell.alpha = 0;
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1];
    cell.alpha = 1;
    [UIView commitAnimations];

    
    //收藏  逻辑
    if (self.array.count != 0) {
        VideoPlayerModel *videoPlayer = [self.array objectAtIndex:indexPath.row];
        
        
        cell.videoPlayer = [self.array objectAtIndex:indexPath.row];
        [[DataHandle shareHandleController] openDB];
        [[DataHandle shareHandleController] createCollectTable];
        NSMutableArray *array = [[DataHandle shareHandleController] selectCollect];
        
        if (array.count == 0) {
            cell.buer = NO;
            
        }else
        {
            for (CollectModel *collect in array) {
                
                NSString *modelId = [NSString stringWithFormat:@"%@",videoPlayer.modelId];
                if ([collect.modelId isEqualToString:modelId]) {
                    cell.buer  = YES;
                    cell.videoPlayer = [self.array objectAtIndex:indexPath.row];
                    
                    return cell;
                }
            }
            cell.buer = NO;
            cell.videoPlayer = [self.array objectAtIndex:indexPath.row];
            
        }
    }

    
    
    return cell;
}


-(void)asker
{
    NSMutableDictionary *askerDic = [NSMutableDictionary dictionary];
    [askerDic setObject:@"0" forKey:@"D-A"];
    [askerDic setObject:self.Id forKey:@"id"];
    [askerDic setObject:@"true" forKey:@"relatedVideos"];
    [askerDic setObject:@"true" forKey:@"supportHtml"];
    
    
    self.array = [NSMutableArray array];
    if ([self.type isEqual:@"VIDEO"]||[self.type isEqualToString:@"PROGRAM"]) {
        
        [Connect ConnectRequestAFWithURL:VIDEODETAIL params:askerDic requestHeader:RequestHeader httpMethod:@"GET" block:^(NSObject *result) {
            NSMutableDictionary *dic = (NSMutableDictionary *)result;
            NSMutableArray *array = [dic objectForKey:@"relatedVideos"];
            
            for (NSMutableDictionary *newDic in array) {
                
                VideoPlayerModel *videoPlayer = [[[VideoPlayerModel alloc] init]autorelease];
                videoPlayer.artistName = [newDic objectForKey:@"artistName"];
                videoPlayer.posterPic = [newDic objectForKey:@"posterPic"];
                videoPlayer.title_label = [newDic objectForKey:@"title"];
                videoPlayer.modelId = [newDic objectForKey:@"id"];
                
                [self.array addObject:videoPlayer];
            }
            
            [self addSubview:self.line];
            [self.myTableView reloadData];
        }];
        
        
    }else if ([self.type isEqual:@"PLAYLIST"]) {
        [Connect ConnectRequestAFWithURL:YueDanDetail params:askerDic requestHeader:RequestHeader httpMethod:@"GET" block:^(NSObject *result) {
            NSMutableDictionary *dic = (NSMutableDictionary *)result;
            NSMutableArray *array = [dic objectForKey:@"videos"];
            
            for (NSMutableDictionary *newDic in array) {
                VideoPlayerModel *videoPlayer = [[[VideoPlayerModel alloc] init]autorelease];
                videoPlayer.artistName = [newDic objectForKey:@"artistName"];
                videoPlayer.posterPic = [newDic objectForKey:@"posterPic"];
                videoPlayer.title_label = [newDic objectForKey:@"title"];
                videoPlayer.modelId = [newDic objectForKey:@"id"];
                
                [self.array addObject:videoPlayer];
            }
            
            [self addSubview:self.line];
            [self.myTableView reloadData];
        }];
    }
    
}








/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
